package com.sanen.flyer.listener;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 键盘监听
 *
 * @author wsse
 * @date 2018-09-06 11:09
 */
public abstract class KeyListener implements java.awt.event.KeyListener {
    //记录当前按下的键
    public List<Integer> list = new ArrayList<>();

    @Override
    public void keyTyped(KeyEvent e) {

    }

    /**
     * 将按下的键,添至集合
     * @param e
     */
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        keyPressed(keyCode);
        int position = getPosition(keyCode);
        if (position == -1) {
            list.add(keyCode);
        }
    }

    /**
     * 将松开的键从集合剔除
     * @param e
     */
    public void keyReleased(KeyEvent e) {
        int position = getPosition(e.getKeyCode());
        if (position != -1) {
            list.remove(position);
        }
    }

    public abstract void keyPressed(int keyCode);

    /**
     * 判断按键是否处于按下状态,按键常量类KeyEvent
     *
     * @param keyCode
     * @return
     */
    public boolean isPress(int keyCode) {
        if (getPosition(keyCode) != -1) return true;
        else return false;
    }

    /**
     * 判断按键是否处于按下状态,仅支持字母a~z,A~Z
     * @param key
     * @return
     */
    public boolean isPress(String key) {
        int keyCode = str2int(key);
        if (getPosition(keyCode) != -1) return true;
        else return false;
    }

    /**
     * 释放所有按下键
     */
    public void clear() {
        list.clear();
    }

    private int getPosition(int code) {
        for (int i = 0; i < list.size(); i++) {
            if (code == list.get(i).intValue()) {
                return i;
            }
        }
        return -1;
    }

    private int str2int(String key){
        int keyCode=0;
        key = key.toUpperCase();
        if ("Q".equals(key)) {
            keyCode = KeyEvent.VK_Q;
        } else if ("W".equals(key)) {
            keyCode = KeyEvent.VK_W;
        } else if ("E".equals(key)) {
            keyCode = KeyEvent.VK_E;
        } else if ("R".equals(key)) {
            keyCode = KeyEvent.VK_R;
        } else if ("T".equals(key)) {
            keyCode = KeyEvent.VK_T;
        } else if ("Y".equals(key)) {
            keyCode = KeyEvent.VK_Y;
        } else if ("U".equals(key)) {
            keyCode = KeyEvent.VK_U;
        } else if ("I".equals(key)) {
            keyCode = KeyEvent.VK_I;
        } else if ("O".equals(key)) {
            keyCode = KeyEvent.VK_O;
        } else if ("P".equals(key)) {
            keyCode = KeyEvent.VK_P;
        } else if ("A".equals(key)) {
            keyCode = KeyEvent.VK_A;
        } else if ("S".equals(key)) {
            keyCode = KeyEvent.VK_S;
        } else if ("D".equals(key)) {
            keyCode = KeyEvent.VK_D;
        } else if ("F".equals(key)) {
            keyCode = KeyEvent.VK_F;
        } else if ("G".equals(key)) {
            keyCode = KeyEvent.VK_G;
        } else if ("H".equals(key)) {
            keyCode = KeyEvent.VK_H;
        } else if ("J".equals(key)) {
            keyCode = KeyEvent.VK_J;
        } else if ("K".equals(key)) {
            keyCode = KeyEvent.VK_K;
        } else if ("L".equals(key)) {
            keyCode = KeyEvent.VK_L;
        } else if ("Z".equals(key)) {
            keyCode = KeyEvent.VK_Z;
        } else if ("X".equals(key)) {
            keyCode = KeyEvent.VK_X;
        } else if ("C".equals(key)) {
            keyCode = KeyEvent.VK_C;
        } else if ("V".equals(key)) {
            keyCode = KeyEvent.VK_V;
        } else if ("B".equals(key)) {
            keyCode = KeyEvent.VK_B;
        } else if ("N".equals(key)) {
            keyCode = KeyEvent.VK_N;
        } else if ("M".equals(key)) {
            keyCode = KeyEvent.VK_M;
        }
        return keyCode;
    }
}
